<?php

namespace app\common\dao\contract;

use think\db\BaseQuery;
use app\common\dao\BaseDao;
use app\common\model\contract\ContractLogModel;

class ContractLogDao extends BaseDao
{

    /**
     * @return ContractLogModel
     */
    protected function getModel(): string
    {
        return ContractLogModel::class;
    }

    /**
     * @param array $where
     * @return BaseQuery
     */
    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['contract_id']) && $where['contract_id'] !== '', function ($query) use ($where) {
                $query->where('contract_id', (int)$where['contract_id']);
            })
            ->when(isset($where['user_id']) && $where['user_id'] !== '', function ($query) use ($where) {
                $query->where('user_id', (int)$where['user_id']);
            })
            ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
                $query->whereLike('remark', '%' . trim($where['keyword']) . '%');
            });
    }

    public function deleteByContractId(int $id)
    {
        return $this->getSearch([
            'contract_id' => $id
        ])->delete();
    }
}
